// pages/register/register.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    userCode: '',
    password: '',
    rePassword: '',
    userShow: false,
  },

  setUserTel(code) {
    if (!/^[a-zA-Z0-9]{4,10}$/.test(code) && code != '') {
      this.setData({
        userShow: true
      })
    } else {
      this.setData({
        userShow: false
      })
    }
  },

  getName(e) {
    setTimeout(() => {
      this.setData({
        username: e.detail
      })
    }, 500)
  },
  getCode(e) {
    setTimeout(() => {
      this.setData({
        userCode: e.detail
      })
      this.setUserTel(this.data.userCode)
    }, 500)
  },
  getPwd(e) {
    setTimeout(() => {
      this.setData({
        password: e.detail
      })
    }, 500)
  },
  getRePwd(e) {
    this.setData({
      rePassword: e.detail
    })
  },

  registerFun() {
    let name = this.data.username
    let code = this.data.userCode
    let pwd = this.data.password
    let repwd = this.data.rePassword
    let showse = this.data.userShow
    console.log("点击了注册")
    console.log("name", name)
    console.log("pwd", pwd)
    console.log("repwd", repwd)
    console.log();

    if (!name || !pwd || !repwd || !code) {
      wx.showToast({
        icon: 'none',
        title: '输入信息不能够空',
      })
      return
    }
    if (showse) {
      wx.showToast({
        icon: 'none',
        title: '账号输入错误'
      })
      return
    }
    if (pwd.length < 6 || repwd.length < 6) {
      wx.showToast({
        icon: 'none',
        title: '密码最少由6位数组成',
      })
      return
    }
    if (pwd != repwd) {
      wx.showToast({
        icon: 'none',
        title: '两次密码不一致',
      })
      return
    }

    let time = Date.now()
    console.log(time);
    wx.cloud.database().collection('user').add({
      data: {
        name: name,
        usercode: code,
        password: pwd,
        registerTime: time,
        img: 'https://i0.hdslb.com/bfs/face/member/noface.jpg@160w_160h_1c_1s.webp',
        sex: '男'
      },
      success(res) {
        console.log('注册成功', res)
        wx.showToast({
          title: '注册成功',
        })
        wx.navigateTo({
          url: '/pages/login/login',
        })
      },
      fail(res) {
        console.log('注册失败', res)
      }
    })
    console.log('注册成功');
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})